package io.mvvm.security.handler;

import com.alibaba.fastjson.JSON;
import io.mvvm.common.enumerate.ResponseEnum;
import io.mvvm.common.entity.AjaxResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: seize-login
 * @description: 拒绝访问处理器
 * @author: 潘
 * @create: 2021-05-14 22:35
 **/
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {

        AjaxResponse response = AjaxResponse.error(ResponseEnum.INSUFFICIENT_PERMISSIONS);
        httpServletResponse.setContentType("application/json; charset=utf-8");

        httpServletResponse.getWriter().write(JSON.toJSONString(response));
    }
}
